/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include "VCModNetClient.h"
#include "VCModNet.h"

VCModNetClientTCPClient::VCModNetClientTCPClient(VCModNetClient & mod)
: VCTCPClient()
, m_mod(mod)
, m_recv_buff(VCTCPCLIENT_BUFF_SIZE)
{
}

VCModNetClientTCPClient::~VCModNetClientTCPClient()
{
}

void VCModNetClientTCPClient::OnRecv(void * data, size_t data_size)
{
	VCAutoMutex lock(m_recv_buff_mutex);
	m_recv_buff.Push(data, data_size);
	size_t recv_buff_size = m_recv_buff.Size();
	if (recv_buff_size >= sizeof(MODNET_HEAD))
	{
		MODNET_HEAD * head = (MODNET_HEAD *)m_recv_buff.GetDataPtr();
		size_t data_size = (size_t)head->data_size;
		if (recv_buff_size >= (sizeof(MODNET_HEAD) + data_size))
		{
			char * data = (char *)m_recv_buff.GetDataPtr() + sizeof(MODNET_HEAD);
			if (m_mod.m_reciver)
			{
				m_mod.m_reciver->Push((VC_CMD)head->cmd, (void *)data, data_size, (VC_DATAFORMAT)head->data_format);
			}
			m_recv_buff.PopSize(sizeof(MODNET_HEAD) + data_size);
		}
	}
}

void VCModNetClientTCPClient::OnDisconect()
{
	m_mod.m_controller.OnNotifyFromMod(m_mod, (VC_NOTIFY)VC_DISCONECT);
}

VCModNetClient::VCModNetClient(VCIController & controller)
: VCIMod(controller)
{
	m_net = new VCModNetClientTCPClient(*this);
	m_controller.OnNotifyFromMod(*this, VC_INFO_INIT);
}

VCModNetClient::~VCModNetClient()
{
	delete m_net;
}

VC_RESULT VCModNetClient::Push(VC_CMD cmd, void * data_buff, size_t data_size, VC_DATAFORMAT data_format)
{
	VC_RESULT result = VC_OK;

	VCAutoMutex lock(m_net_send_mutex);
	MODNET_HEAD head;
	head.cmd = (long)cmd;
	head.data_size = (unsigned long)data_size;
	head.data_format = (long)data_format;
	m_net->Send(&head, sizeof(MODNET_HEAD));
	m_net->Send(data_buff, data_size);

	return result;
}

